// utils/routeGenerator.ts
import type { MenuNode } from "@/types/menu";
import type { RouteRecordRaw } from "vue-router";
import SysMenu from "@/views/System/SysMenu.vue";
import DynamicPage from "@/views/DynamicPage.vue";
import SysDict from "@/views/System/SysDict.vue";
import SysForm from "@/views/System/SysForm.vue";
import ThreePage from "@/views/System/ThreePage.vue";
import DoubleTable from "@/views/System/DoubleTable.vue";
import AppManage from "@/views/System/AppManage.vue";
import PlanScheduling from "@/views/Customized/PlanScheduling.vue"

// 路径处理工具
function hrefToPathSegments(href: string): string[] {
  const trimmed = href.trim();

  const classNameMatch = trimmed.match(/[?&]className=([^&]+)/);
  const className = classNameMatch
    ? decodeURIComponent(classNameMatch[1])
    : null;

  if (className) {
    const pathPart = trimmed.split("?")[0]; // 去掉查询参数
    const segments = pathPart.split("/").filter(Boolean);
    if (segments.length > 0) {
      const module = segments[0]; // 一级目录
      return [module, className];
    }
  }

  return trimmed
    .split("?")[0]
    .replace(/\.html$/, "")
    .split("/")
    .filter(Boolean)
    .slice(0, 2);
}

function segmentsToPath(segments: string[]): string {
  return "/" + segments.join("/");
}

// 将菜单树转为路由
export function generateRoutesFromMenu(menuList: MenuNode[]): RouteRecordRaw[] {
  const routes: RouteRecordRaw[] = [];

  menuList.forEach((item) => {
    let pathSegments: string[] = [];

    if (item.href ? item.href:item.vueHref) {
      pathSegments = hrefToPathSegments(item.href ? item.href:item.vueHref);
    } else if (item.children?.length > 0) {
      const firstChildSegments = hrefToPathSegments(item.children[0]?.href);
      pathSegments =
        firstChildSegments.length > 0 ? [firstChildSegments[0]] : [];
    }

    if (pathSegments.length === 0) return;

    const route: RouteRecordRaw = {
      name: item.id,
      title: item.title,
      icon: item.icon,
      meta: {
        title: item.title,
        showMenu: item.hidden !== "1",
        icon: item.icon,
        permission:item.children
      },
    };
    const isLeaf = item.href && item.href.trim() !== "";

    if (isLeaf) {
      route.path = segmentsToPath(pathSegments);
      if (route.path === "/system/menu") {
        route.component = SysMenu;
      } else if (route.path === "/system/user") {
        route.component = SysDict;
      } else if (route.path === "/system/form") {
        route.component = SysForm;
      } else if (
        route.path === "/system/area" ||
        route.path === "/system/dept"
      ) {
        route.component = ThreePage;
      } else if (route.path === "/system/dict") {
        route.component = DoubleTable;
      } else if(route.path === '/system/package') {
        route.component = AppManage
      } else {
        route.component = DynamicPage;
      }
    } else {
      route.path = `/${pathSegments[0]}`;
      console.log(route.path)
      if(route.path === '/play'){
         route.component = PlanScheduling
      } 
      const children = generateRoutesFromMenu(item.children || []);
      if (children.length > 0) {
        route.children = children;
        const firstChildPath = children[0]?.path;
        if (firstChildPath) {
          route.redirect = `/${pathSegments[0]}${firstChildPath}`;
        }
      }
    }

    routes.push(route);
  });

  return routes;
}
